{include file="admin@public/header" /}

<link href="/public/static/admin/blue/bills.css?ver=20210803202401" rel="stylesheet" type="text/css">

<script src="/public/static/admin/js/common/base.js"></script>
<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">

<style>
    /*#barCodeInsert{margin-left: 10px;font-weight: 100;font-size: 12px;color: #fff;background-color: #B1B1B1;padding: 0 5px;border-radius: 2px;line-height: 19px;height: 20px;display: inline-block;}*/
    /*#barCodeInsert.active{background-color: #23B317;}*/
    .shadow.opera-log-box::before,
    .shadow.opera-log-box::after {
        border: solid transparent;
        content: ' ';
        height: 0;
        bottom: 100%;
        position: absolute;
        width: 0;
    }

    .shadow.opera-log-box:after {
        border-width: 8px;
        border-bottom-color: #FFEED4;
        right: 60px;
    }

    .shadow.opera-log-box:before {
        border-width: 10px;
        border-bottom-color: #E1B77E;
        right: 58px;
    }

    .con-header dd {
        margin-right: 30px;
    }

    .grid-wrap h5 {
        padding: 10px 0;
        font-weight: bold;
        margin-top: 10px;
    }

    #tempName {
        width: 160px;
    }

    .qtyInsert {
        margin-left: 8px;
        font-weight: 100;
        font-size: 12px;
        color: #fff;
        background-color: #B1B1B1;
        padding: 0 5px;
        border-radius: 2px;
        line-height: 19px;
        height: 20px;
        display: inline-block;
    }

    .qtyInsertAfter {
        margin-right: 8px;
        font-weight: 100;
        font-size: 12px;
        color: #fff;
        background-color: #23B317;
        padding: 0 5px;
        border-radius: 2px;
        line-height: 19px;
        height: 20px;
        display: inline-block;
    }

    .con-header dd {
        margin-right: 30px;
    }

    .ui-jqgrid .ui-jqgrid-titlebar-close {
        left: 75px !important;
        margin: 2px 0 0 5px;
    }

    .ui-widget-content .ui-icon.ui-icon-circle-triangle-s {
        background: url(/css/img/down-arrow-btn.png);
        display: inline-block;
        width: 16px;
        height: 16px;
    }

    .ui-widget-content .ui-icon.ui-icon-circle-triangle-n {
        background: url(/css/img/up-arrow-btn.png);
        display: inline-block;
        width: 16px;
        height: 16px;
    }

    .ui-jqgrid .ui-jqgrid-titlebar-close span {
        margin: 0;
    }

    .ui-jqgrid .ui-jqgrid-titlebar {
        padding: 0;
    }

    .ui-jqgrid .ui-jqgrid-titlebar-close:hover {
        padding: 1px;
    }

    .showBox {
        display: inline-block;
        background: url(/public/static/admin/css/img/up-arrow-btn.png);
    }

    .hideBox {
        display: inline-block;
        background: url(/public/static/admin/css/img/down-arrow-btn.png);
    }
</style>

<div class="wrapper">
    <!-- <span id="config" class="ui-icon ui-state-default ui-icon-config"></span> -->
    <div class="mod-toolbar-top mr0 cf dn" id="toolTop"></div>
    <div class="bills cf">
        <div class="con-header">
            <dl class="cf">
                <dd>
                    <label>单据日期：</label>
                    <input type="date" id="date" class="ui-input" value="" style="width: 130px;">
                </dd>
                <dd id="identifier" style="float:right">
                    <label>单据编号:</label>
                    <span id="sn">{$info.sn}</span>
                    <i id="editBills"></i>
                    <span class="ui-combo-wrap" id="numberAuto" style="display: none;">
                        <i class="trigger"></i>
                    </span>
                </dd>
                <dd class="tc">
                    <label>组装费用:</label>
                    <input type="text" id="amount" class="ui-input" data-ref="discount" value="0.00" placeholder="0.00">
                </dd>
            </dl>
            <hr class="hrcls" />
            <div class="cf  ">
                <div class="fl">
                    <span id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a>列设置</span>
                    <span><label>扫描枪录入</label><span id="barCodeInsert" class="close"><span
                                class="ui-icon-circle"></span></span></span>
                    <a id="chooseTemp" class="ui-btn" onclick="chooseTemplate()">选择模板</a>
                    <a id="saveTemp" class="ui-btn" onclick="saveTemplate()">存为模板</a>
                </div>
            </div>
        </div>

        <div class="wrapper_con">
            <!-- <span id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a></span> -->

            <!-- 表格 -->
            <div class="grid-wrap">
                <h5>组合件：</h5>
                <table id="fixedGrid"></table>

                <h5>子件：</h5>
                <table id="grid">
                    <div class="ui-jqgrid-titlebar ui-jqgrid-caption ui-widget-header ui-corner-top ui-helper-clearfix"
                        style="border-top:1px solid #ddd;padding:5px 0;">
                        <span class="ui-jqgrid-title">显示/隐藏子件</span>
                        <a role="link" class="ui-jqgrid-titlebar-close ui-corner-all HeaderButton" id="HeaderButton"
                            style="right: 0px;">
                            <span class="ui-icon ui-icon-circle-triangle-n showBox"></span>
                        </a>
                    </div>
                </table>
            </div>

            <div class="cf" id="bottomField">
                <div class="fr" id="toolBottom">
                    <a id="savaAndAdd" class="ui-btn ui-btn-sp mrb">保存并新增</a>
                    <a id="save" class="ui-btn">保存</a>
                    <a class="ui-btn" id="audit">审核</a>
                </div>
            </div>
        </div>

        <div class="con-footer cf">
            <div class="mb10">
                <textarea type="text" id="note" class="ui-input ui-input-ph" placeholder="暂无备注信息"></textarea>
            </div>
            <div class="cf fr">
                <a href="#" class="linkToCheck ui-btn-bill ui-btn-add mr20">历史单据</a>
                <a href="#" id="operaLog" class="ui-btn">操作日志</a>
            </div>
        </div>


        <div id="operaLogBox" class="shadow opera-log-box" style="position: absolute;z-index: 999; display: none;">
            <p class="cf">
            <div class=" mr30"><label>制单人：</label><span id="userName"><span open-data></span></span></div>
            <div class=""><label>制单时间：</label><span id="createTime"></span></div>
            </p>
            <p class="cf">
            <div class=" mr30"><label>最后修改人：</label><span id="modifyName">黄小姐</span></div>
            <div class=""><label>最后修改时间：</label><span id="modifyTime"></span></div>
            </p>
        </div>
    </div>



    <!--  以隐藏的方式获取商品的样式 ，作用是样式 -->
    <div id="pelem_goods" style="display:none;"> </div>
    <div id="pelem_goods_child" style="display:none;"> </div>
    <div id="p_store_id" style="display: none"></div>
    <div id="p_store_id_child" style="display: none"></div>

    <!--  以隐藏的方式获取商品的样式 ，作用是赋值 -->
    <div style="display:none;">
        <input type="text" id="id" value="{$id}" autocomplete="off" readonly="">
        <input type="text" id="goodsname" value="" autocomplete="off" readonly="">
        <input type="text" id="store_name" value="" autocomplete="off" readonly="">

        <input type="text" id="goodsname_child" value="" autocomplete="off" readonly="">
        <input type="text" id="store_name_child" value="" autocomplete="off" readonly="">
    </div>

    <!-- 仓库弹窗(组合件) -->
    <div class="ui-droplist-wrap" id="ck_wrap" style="position: absolute; z-index: 1000; width: 102px;display:none;">
        <div class="droplist" id="ck_list"
            style="position: relative; overflow: auto; min-height: 150px; height: 150px;">
            {volist name="store" id="vo"}
            <div class="list-item" store_id="{$vo.id}" title="{$vo.name}">{$vo.name}</div>
            {/volist}
        </div>
    </div>

    <!-- 仓库弹窗(子件) -->
    <div class="ui-droplist-wrap" id="ck_wrap_child"
        style="position: absolute; z-index: 1000; width: 102px;display:none;">
        <div class="droplist" id="ck_list"
            style="position: relative; overflow: auto; min-height: 150px; height: 150px;">
            {volist name="store" id="vo"}
            <div class="list-item" store_id="{$vo.id}" title="{$vo.name}">{$vo.name}</div>
            {/volist}
        </div>
    </div>
</div>

<!-- 组合件 -->
<script type="text/javascript">
    var edit_row = 0;
    var edit_col = 0;
    var row_id = 0;
    var col_name = 0;

    $(document).ready(function () {
        var inventory = '可用库存<img src="/public/static/admin/css/img/help.png" style="width: 20px;display: inline-block;margin-left: 5px;" onclick="snapshotTipsFun()" id="snapshotTipsBtn"/>';
        $("#fixedGrid").jqGrid({
            url: '/store/assembly/edit.html?ajax=100',
            datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
            height: '90%',//高度，表格高度。可为数值、百分比或'auto'
            //width: Public.setGrid().w,
            //height: Public.setGrid().h,
            colNames: ["goods_id", "<span class='red'>*</span>商品", "商品条码", "单位", "<span class='red'>*</span>仓库", inventory, "产地", "注册证号", "生产许可证", "<span class='red'>*</span>数量", "入库单位成本", "入库成本", "备注", "仓库id", "单位id"],
            colModel: [
                { name: 'goods_id', hidden: true },
                {
                    name: "goodsname",
                    label: "商品",
                    width: 370,
                    title: !0,
                    classes: "goods",
                    editable: true,
                    edittype: "custom",
                    editoptions: {
                        custom_element: function (t, e) {
                            $("#pelem_goods").html('<div class="pr" id="elem_goods"><input type="text" class="textbox  customelement" name="goodsname" autocomplete="off" ><span class="ui-icon-ellipsis"></span></div>');
                            return $("#elem_goods");
                        },
                        custom_value: function (t, e, a) {
                            var ad = $("#goodsname").val();
                            $("#goodsname").val("");//重置赋值
                            return ad;
                        },
                        trigger: "ui-icon-ellipsis"
                    }
                },
                { name: 'goods_tm', index: '', label: "商品条码", width: 120, editable: false, hidden: true },
                { name: 'dw', index: '', label: "单位", width: 80, editable: false },
                {
                    name: "store_name",
                    width: 100,
                    label: "仓库",
                    link: "inventory",
                    editable: true,
                    edittype: "custom",
                    editoptions: {
                        custom_element: function (t, e) {
                            $("#p_store_id").html('<div class="pr" id="store_id"><input type="text" class="textbox storageAuto customelement ui-combo-active" name="locationName" autocomplete="off" id="1_locationName" style="outline: -webkit-focus-ring-color auto 5px;"><span class="ui-icon-triangle-1-s" id="st_id"></span></div>');
                            return $("#store_id");
                        },
                        custom_value: function (t, e, a) {
                            var ba = $("#store_name").val();
                            $("#store_name").val("");
                            return ba;
                        },

                        trigger: "ui-icon-triangle-1-s"
                    }
                },
                { name: 'use_ck', index: '', label: "可用库存", width: 80, align: "center", editable: false },
                { name: '', index: '', label: "产地", width: 90, editable: false, hidden: true, formatter: setdata },
                { name: '', index: '', label: "注册证号", width: 90, editable: false, hidden: true },
                { name: '', index: '', label: "生产许可证", width: 90, editable: false, hidden: true },
                { name: 'number', index: '', label: "数量", width: 80, align: "right", editable: true },
                { name: 'rkdwcb', index: '', label: "入库单位成本", width: 100, align: "right", editable: false },
                { name: '', index: '', label: "入库成本", width: 100, align: "right", editable: false },
                { name: 'mark', index: '', label: "备注", width: 150, editable: true },
                { name: 'store_id', index: '', label: "仓库id", width: 150, editable: false, hidden: true },
                { name: 'unit_id', label: "单位id", width: 150, hidden: true },
            ],
            cellEdit: true,
            cellurl: '/',
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'id',
            sortorder: "desc",
            pagerpos: "left",//分页位置
            rownumbers: true, //自增序号
            pgbuttons: true,//翻页按钮
            pagination: true,
            altRows: false,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            pager: "#page",
            viewrecords: !0,
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            },
            beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
                col_name = cellname;
                edit_row = iRow;
                edit_col = iCol;
                row_id = rowid;
                var gname = $("#fixedGrid").jqGrid("getCell", row_id, 'goodsname');
                $("#goodsname").val(gname);
            }
        });

        var mydata = [];
        for (var i = 0; i < 1; i++) {
            var obj = { goodsname: "" }
            mydata.push(obj);
        };
        for (var i = 0; i <= mydata.length; i++) {
            jQuery("#fixedGrid").jqGrid('addRowData', i + 1, mydata[i]);
        }

    });

    function setdata(cellvalue, options, rowObject) {
        if (rowObject.son) {
            for (let i = 0; i < rowObject.son.length; i++) {
                const element = rowObject.son[i];
                $("#grid").jqGrid("saveCell", edit_row, edit_col);
                $("#grid").jqGrid("setCell", i + 1, 'goods_id', element.goods_id);
                $("#grid").jqGrid("setCell", i + 1, 'goodsname', element.goodsname);
                $("#grid").jqGrid("setCell", i + 1, 'dw', element.unit_name);
                $("#grid").jqGrid("setCell", i + 1, 'use_ck', element.use_ck);
                $("#grid").jqGrid("setCell", i + 1, 'number', element.number);
                $("#grid").jqGrid("setCell", i + 1, 'rkdwcb', 0);
                $("#grid").jqGrid("setCell", i + 1, 'unit_id', element.unit_id);
                $("#grid").jqGrid("setCell", i + 1, 'store_id', element.store_id);
                $("#grid").jqGrid("setCell", i + 1, 'store_name', element.store_name);
            }
        }
    }

</script>

<!-- 子件 -->
<script type="text/javascript">
    var edit_row_child = 0;
    var edit_col_child = 0;
    var row_id_child = 0;
    var col_name_child = 0;
    $(document).ready(function () {

        var batch = '<span class="red">*</span>仓库<small id="batchStorage1" onclick="batchBtn(1);" class="btns">批量</small>';
        var inventory = '可用库存<img src="/public/static/admin/css/img/help.png" style="width: 20px;display: inline-block;margin-left: 5px;" onclick="snapshotTipsFun()" id="snapshotTipsBtn"/>';

        $("#grid").jqGrid({
            // url: '/admin/unit/multiindex.html?ajax=100',
            datatype: "local",//数据来源，本地数据（local，json,jsonp,xml等）
            height: '90%',//高度，表格高度。可为数值、百分比或'auto'
            //width: Public.setGrid().w,
            //height: Public.setGrid().h,
            colNames: ["id", "操作", "<span class='red'>*</span>商品", "商品条码", "单位", batch, inventory, "产地", "注册证号", "生产许可证", "<span class='red'>*</span>数量", "出库单位成本", "出库成本", "备注", "仓库id", "单位id"],
            colModel: [
                { name: 'goods_id', index: 'id', hidden: true },
                { name: 'id', index: 'id', width: 60, formatter: cLink },
                {
                    name: "goodsname",
                    label: "商品",
                    width: 330,
                    title: !0,
                    classes: "goods",
                    editable: true,
                    edittype: "custom",
                    editoptions: {
                        custom_element: function (t, e) {
                            $("#pelem_goods_child").html('<div class="pr" id="elem_goods_child"><input type="text" class="textbox  customelement" name="goodsname" autocomplete="off" ><span class="ui-icon-ellipsis"></span></div>');
                            return $("#elem_goods_child");
                        },
                        custom_value: function (t, e, a) {
                            var ad = $("#goodsname_child").val();
                            $("#goodsname_child").val("");//重置赋值
                            return ad;
                        },
                        trigger: "ui-icon-ellipsis"
                    }
                },
                { name: '', index: '', label: "商品条码", width: 120, editable: false, hidden: true },
                { name: 'dw', index: '', label: "单位", width: 80, editable: false },
                {
                    name: "store_name",
                    width: 100,
                    label: "仓库",
                    link: "inventory",
                    editable: true,
                    edittype: "custom",
                    editoptions: {
                        custom_element: function (t, e) {
                            $("#p_store_id_child").html('<div class="pr" id="store_id_child"><input type="text" class="textbox storageAuto customelement ui-combo-active" name="locationName" autocomplete="off" id="1_locationName" style="outline: -webkit-focus-ring-color auto 5px;"><span class="ui-icon-triangle-1-s" id="st_id_child"></span></div>');
                            return $("#store_id_child");
                        },
                        custom_value: function (t, e, a) {
                            var ba = $("#store_name_child").val();
                            $("#store_name_child").val("");
                            return ba;
                        },

                        trigger: "ui-icon-triangle-1-s"
                    }
                },
                { name: 'use_ck', index: '', label: "可用库存", width: 80, editable: false },
                { name: '', index: '', label: "产地", width: 90, editable: false, hidden: true },
                { name: '', index: '', label: "注册证号", width: 90, editable: false, hidden: true },
                { name: '', index: '', label: "生产许可证", width: 90, editable: false, hidden: true },
                { name: 'number', index: '', label: "数量", width: 80, align: "right", editable: true },
                { name: '', index: '', label: "出库单位成本", width: 100, editable: false },
                { name: 'chuku_money', index: '', label: "出库成本", width: 100, align: "right", editable: false },
                { name: 'mark', index: '', label: "备注", width: 150, editable: true },
                { name: 'store_id', index: '', label: "仓库id", width: 150, editable: false, hidden: true },
                { name: 'unit_id', index: '', label: "单位id", width: 150, hidden: true }
            ],
            cellEdit: true,
            cellurl: '/',
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'id',
            sortorder: "desc",
            pagerpos: "left",//分页位置
            rownumbers: true, //自增序号
            pgbuttons: true,//翻页按钮
            pagination: true,
            altRows: false,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            pager: "#page1",
            viewrecords: !0,
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            },
            beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
                col_name_child = cellname;
                edit_row_child = iRow;
                edit_col_child = iCol;
                row_id_child = rowid;
                var gname = $("#grid").jqGrid("getCell", row_id_child, 'goodsname');
                $("#goodsname_child").val(gname);
            },
            // 合计
            footerrow: true,
            gridComplete: function () {
                var chuku_money = parseFloat($("#grid").getCol('chuku_money', false, 'sum')).toFixed(2);
                $("#grid").footerData('set', { "goodsname": '合计：', chuku_money: chuku_money });
            },
            beforeSaveCell: function (rowid, cellname, value, iRow, iCol) {
                edit_row_child = iRow;
                edit_col_child = iCol;
                row_id_child = rowid;

                var shuliangs = parseFloat($("#grid").getCol('shuliang', false, 'sum')).toFixed(2);
                var chuku_moneys = parseFloat($("#grid").getCol('chuku_money', false, 'sum')).toFixed(2);
                $("#grid").footerData('set', { "goodsname": '合计：', shuliang: shuliangs, chuku_money: chuku_moneys });
            }
        });
        var mydata = [];
        for (var i = 0; i < 5; i++) {
            var obj = { goodsname: "" }
            mydata.push(obj);
        };
        for (var i = 0; i <= mydata.length; i++) {
            jQuery("#grid").jqGrid('addRowData', i + 1, mydata[i]);
        }
    });

    //增加、删除按钮
    function cLink(cellvalue, options, rowObject) {
        return '<div class="operating"><span class="ui-icon ui-icon-plus" title="新增行" onclick="add_row(' + options.rowId + ')" style="margin:0 2px;"></span> <span class="ui-icon ui-icon-trash" onclick="del_row(' + options.rowId + ')" title="删除行" style="margin:0 2px;">删除</span></div>';
    }

</script>

<script type="text/javascript">
    $(document).ready(function () {
        // 选择仓库弹窗(组件)
        $("#fixedGrid").on("click", ".ui-icon-triangle-1-s", function (e) {
            var top = $(this).offset().top;
            var left = $(this).offset().left;
            $("#ck_wrap").slideToggle();
            $("#ck_wrap").css({ "top": (top + 22) + "px", "left": (left - 78) + "px" })
        })
        //选择仓库赋值（组件）
        $("#ck_wrap #ck_list").on("click", ".list-item", function (e) {
            var title = $(this).attr("title");
            var store_id = $(this).attr("store_id");
            var goods_id = $("#grid").jqGrid("getCell", row_id, 'id');
            //查询库存

            $.get("/admin/basic/get_stock.html", { store_id: store_id, goods_id: goods_id }, function (data, state) {
                if (data.status == 1) {
                    $("#fixedGrid").jqGrid("setCell", row_id, 'use_ck', data.info);
                    $("#fixedGrid").jqGrid("saveCell", edit_row, edit_col);
                } else {
                    layer.msg("获取仓库失败");
                }
            })

            $("#store_name").val(title);
            $("#fixedGrid").jqGrid("setCell", row_id, 'store_id', store_id);
            $("#fixedGrid").jqGrid("setCell", row_id, 'store_name', title);//及时保存
            $("#fixedGrid").saveRow(row_id, false);
            $("#ck_wrap").hide();
        });

        // 选择仓库弹窗(子件)
        $("#grid").on("click", ".ui-icon-triangle-1-s", function (e) {
            var top = $(this).offset().top;
            var left = $(this).offset().left;
            $("#ck_wrap_child").slideToggle();
            $("#ck_wrap_child").css({ "top": (top + 22) + "px", "left": (left - 78) + "px" })
        })
        //选择仓库赋值（子件）
        $("#ck_wrap_child #ck_list").on("click", ".list-item", function (e) {
            var title = $(this).attr("title");
            var store_id = $(this).attr("store_id");
            var goods_id = $("#grid").jqGrid("getCell", row_id_child, 'goods_id');
            //查询库存
            $.get("/admin/basic/get_stock.html", { store_id: store_id, goods_id: goods_id }, function (data, state) {
                if (data.status == 1) {
                    $("#grid").jqGrid("setCell", row_id_child, 'use_ck', data.info);
                    $("#grid").jqGrid("saveCell", edit_row_child, edit_col_child);
                } else {
                    layer.msg("获取仓库失败");
                }
            })
            $("#store_name_child").val(title);
            $("#grid").jqGrid("setCell", row_id_child, 'store_id_child', store_id);
            $("#grid").jqGrid("setCell", row_id_child, 'store_id', store_id);
            $("#grid").jqGrid("setCell", row_id_child, 'store_name', title);//及时保存
            $("#grid").saveRow(row_id_child, false);
            $("#ck_wrap_child").hide();
        });
    })
    //仓库选择框外点击则隐藏
    $(document).mouseup(function (e) {
        // 组合件
        if (e.target.id != "st_id") {
            var con = $("#ck_wrap");   // 设置目标区域
            if (!con.is(e.target) && con.has(e.target).length === 0) {
                $("#ck_wrap").hide();
            }
        }
        // 子件
        if (e.target.id != "st_id_child") {
            var con = $("#ck_wrap_child");   // 设置目标区域
            if (!con.is(e.target) && con.has(e.target).length === 0) {
                $("#ck_wrap_child").hide();
            }
        }
    });

</script>

<script type="text/javascript">
    //选择商品1-1
    $(document).ready(function () {
        //获取商品
        $("#fixedGrid").on("click", ".ui-icon-ellipsis", function (e) {
            pre_choose_goods('/admin/basic/choose_goods_one.html', this);
        });
    });
    //选择商品1-2
    function pre_choose_goods(url, obj) {
        choose_goods(url, obj, function (res) {
            if (res.btn == 3 && res.status == 1) {
                //console.log(res);
                var data = res.data;
                if (data.status == 1) {

                    var $count = data.info.count;
                    var good_list = data.info.list;
                    var allIds = $("fixedGrid").jqGrid('getDataIDs');
                    var ids = $("#fixedGrid").getDataIDs();

                    // console.log("当前行");
                    // console.log(row_id);
                    // console.log("长度："+ids.length);
                    // console.log(ids);
                    row_id = parseInt(row_id);
                    var $cha_id = ids.length - row_id;//最后id-当前id
                    var cha_han = $count - $cha_id - 1;//需要补充的行
                    //console.log("cha:"+cha_han);
                    //补充行
                    // if(cha_han>0){
                    //     var mydata1 = [];
                    //     for (var iii = ids.length; iii < ids.length+cha_han; iii++) {
                    //         var obj = { id: iii + 1, goodsname: "", goods_tm: "", dw: "", store_name: "", use_ck: "", shulaing:"",rkdwcb:""}
                    //         mydata1.push(obj);
                    //     }
                    //         console.log(mydata1);
                    //         for (var iii = 0; iii <= mydata1.length; iii++) {
                    //         jQuery("#fixedGrid").jqGrid('addRowData', ids.length+iii+1, mydata1[iii]);
                    //     }

                    // }
                    for (let i = 0; i < $count; i++) {
                        // $("#goodsname").val(data.info.name);
                        //alert(row_id);
                        $("#fixedGrid").jqGrid("saveCell", edit_row, edit_col);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'goods_id', good_list[i].id);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'goodsname', good_list[i].name);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'dw', good_list[i].dw);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'use_ck', 0);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'number', 1);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'rkdwcb', 0);
                        $("#fixedGrid").jqGrid("setCell", row_id, 'unit_id', good_list[i].unit);

                    }
                } else {
                    layer.msg("获取商品信息失败");
                }
            }
        });
    }

    //选择商品2-1
    $(document).ready(function () {
        //获取商品
        $("#grid").on("click", ".ui-icon-ellipsis", function (e) {
            pre_choose_goods_child('/admin/basic/choose_goods.html', this);
        });
    });
    //选择商品2-2
    function pre_choose_goods_child(url, obj) {
        choose_goods(url, obj, function (res) {
            if (res.btn == 3 && res.status == 1) {
                //console.log(res);
                var data = res.data;
                if (data.status == 1) {

                    var $count = data.info.count;
                    var good_list = data.info.list;
                    var allIds = $("grid").jqGrid('getDataIDs');
                    var ids = $("#grid").getDataIDs();

                    // console.log("当前行");
                    // console.log(row_id);
                    // console.log("长度："+ids.length);
                    // console.log(ids);
                    row_id_child = parseInt(row_id_child);
                    var $cha_id = ids.length - row_id_child;//最后id-当前id
                    var cha_han = $count - $cha_id - 1;//需要补充的行
                    //console.log("cha:"+cha_han);
                    //补充行
                    if (cha_han > 0) {
                        var mydata1 = [];
                        for (var iii = ids.length; iii < ids.length + cha_han; iii++) {
                            var obj = { id: iii + 1, goodsname: "", goods_tm: "", dw: "", store_name: "", use_ck: "", shulaing: "", chuku_money: "" }
                            mydata1.push(obj);
                        }
                        console.log(mydata1);
                        for (var iii = 0; iii <= mydata1.length; iii++) {
                            jQuery("#grid").jqGrid('addRowData', ids.length + iii + 1, mydata1[iii]);
                        }
                    }

                    for (let i = 0; i < $count; i++) {
                        // $("#goodsname_child").val(data.info.name);
                        // alert(row_id);
                        $("#grid").jqGrid("saveCell", edit_row_child, edit_col_child);
                        $("#grid").jqGrid("setCell", row_id_child + i, 'goods_id', good_list[i].id);
                        $("#grid").jqGrid("setCell", row_id_child + i, 'goodsname', good_list[i].name);
                        $("#grid").jqGrid("setCell", row_id_child + i, 'dw', good_list[i].dw);
                        $("#grid").jqGrid("setCell", row_id_child + i, 'shuliang', 1);
                        $("#grid").jqGrid("setCell", row_id_child + i, 'chuku_money', 0);
                        $("#grid").jqGrid("setCell", row_id_child + i, 'unit_id', good_list[i].unit);
                    }

                    // 合计的变量
                    var shuliangs = parseFloat($("#grid").getCol('shuliang', false, 'sum')).toFixed(2);
                    var chuku_moneys = parseFloat($("#grid").getCol('chuku_money', false, 'sum')).toFixed(2);
                    $("#grid").footerData('set', { "goodsname": '合计：', shuliang: shuliangs, chuku_money: chuku_moneys });
                } else {
                    layer.msg("获取商品信息失败");
                }
            }
        });
    }

</script>

<script type="text/javascript">
    $(document).ready(function () {
        // 显示隐藏子件
        $("#HeaderButton").on("click", function () {
            $("#gbox_grid").toggle();
            if ($(this).children(".ui-icon-circle-triangle-n").hasClass("showBox")) {
                $(this).children(".ui-icon-circle-triangle-n").addClass("hideBox").removeClass("showBox");
            } else {
                $(this).children(".ui-icon-circle-triangle-n").addClass("showBox").removeClass("hideBox");
            }
        });



        // 获取当前日期
        var myDate = new Date;
        var year = myDate.getFullYear(); //获取当前年
        var mon = myDate.getMonth() + 1; //获取当前月
        var date = myDate.getDate(); //获取当前日
        // console.log(year + '-' + mon + '-' + date)
        if (mon < 10) {
            mon = "0" + mon;
        }
        if (date < 10) {
            date = "0" + date;
        }
        $("#date").val(year + '-' + mon + '-' + date);


        // 操作日志鼠标移入移除
        $("#operaLog").hover(function () {
            var top = $(this).offset().top;
            // var left = $(this).offset().left;
            $("#operaLogBox").show();
            $("#operaLogBox").css({ "right": "30px", "top": (top + 20) + "px" })
        }, function () {
            $("#operaLogBox").hide();
        })

        // 扫描枪录入开关
        $("#barCodeInsert").on("click", function () {
            if ($(this).hasClass("close")) {
                $(this).addClass('open');
                $(this).removeClass('close');//removeClass
            } else if ($(this).hasClass("open")) {
                $(this).addClass('close');
                $(this).removeClass('open');//removeClass
            }
        });

    });

</script>

<script>
    $(".wrapper").on("click", "#savaAndAdd", function (e) {
        //先保存
        if (edit_row != 0 && edit_col != 0) {
            $("#grid").jqGrid("saveCell", edit_row, edit_col);
            edit_row = 0;
            edit_col = 0;
        }
        savedata();
    })

    function savedata() {
        var fixedGrid = getJQAllData($("#fixedGrid"));
        var sn = $('#sn').text();
        var addtime = $('#date').val();
        var money = $('#amount').val();
        var grid = getJQAllData($('#grid'));
        var id = $('#id').val();

        $.ajax({
            type: 'POST',
            url: "update.html",
            data: { id: id, sn: sn, addtime: addtime, money: money, fixedGrid: fixedGrid, grid: grid },
            dataType: "json",
            async: false,
            error: function (request) {
                layer.msg("发送请求失败！");
            },
            success: function (data) {
                if (data.status == 1) {
                    layer.msg(data.msg);
                } else {
                    layer.msg(data.msg);
                }

            }
        });

    }
    function getJQAllData(obj) {
        //拿到grid对象
        // var obj = $("#fixedGrid");
        //获取grid表中所有的rowid值
        var rowIds = obj.getDataIDs();
        //初始化一个数组arrayData容器，用来存放rowData
        var arrayData = new Array();
        if (rowIds.length > 0) {
            for (var i = 0; i < rowIds.length; i++) {
                if (Object.values(obj.getRowData(rowIds[i]))[0] != '') {
                    arrayData.push(obj.getRowData(rowIds[i]));
                }
                //rowData=obj.getRowData(rowid);//这里rowid=rowIds[i];
                // console.log(obj.getRowData(rowIds[i].goodsname));
                // return false;
                // if (obj.getRowData(rowIds[i].goodsname != '')) {
                //   arrayData.push(obj.getRowData(rowIds[i]));
                // }
            }
        }
        return arrayData;
    }
</script>


<script type="text/javascript">
    function initializegrid() {
        $("#grid").jqGrid('clearGridData');
        var mydata = [];
        for (var i = 0; i < 5; i++) {
            var obj = { goodsname: "" }
            mydata.push(obj);
        };
        for (var i = 0; i <= mydata.length; i++) {
            jQuery("#grid").jqGrid('addRowData', i + 1, mydata[i]);
        }
    }
    // 选择模板
    function chooseTemplate() {
        layer.open({
            type: 2,
            title: "选择模板",
            shadeClose: true,
            shade: 0.3,
            maxmin: false, //开启最大化最小化按钮
            area: ['780px', '570px'],
            fixed: false, //不固定
            btn: ['确定', '取消'],
            content: "/store/assembly/template.html",
            btn1: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                //获取所有选中行
                // var ids = body.find("#grid").jqGrid('getGridParam', 'selarrrow');
                var ids = body.find("#templateID").val();
                // var templateId = body.find("#grid").jqGrid('getRowData',rowId);
                if (ids == '' || ids == null) {
                    layer.msg("请选择模板");
                    return false;
                } else {
                    initializegrid();
                    // 获取模板信息
                    $.get("/admin/basic/one_templates.html", { id: ids }, function (data, state) {
                        if (data.status == 1) {
                            row_id = 1;
                            $('#fixedGrid').jqGrid('setCell', row_id, 'goods_id', data.list.goods_id);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'goodsname', data.list.goods_name);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'mark', data.list.mark);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'number', data.list.number);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'store_name', data.list.store_name);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'store_id', data.list.store_id);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'unit_id', data.list.unit_id);
                            $('#fixedGrid').jqGrid('setCell', row_id, 'dw', data.list.unit_name);
                            $.get('/admin/basic/get_stock.html', { store_id: data.list.store_id, goods_id: data.list.goods_id }, function (res) {
                                if (res.status == 1) {
                                    $('#fixedGrid').jqGrid('setCell', row_id, 'use_ck', res.info);
                                }
                            });

                            for (let i = 0; i < data.details.length; i++) {
                                $("#grid").jqGrid("saveCell", edit_row, edit_col);
                                $("#grid").jqGrid("setCell", i + 1, 'goods_id', data.details[i].goods_id);
                                $("#grid").jqGrid("setCell", i + 1, 'goodsname', data.details[i].goods_name);
                                $("#grid").jqGrid("setCell", i + 1, 'store_name', data.details[i].store_name);
                                $("#grid").jqGrid("setCell", i + 1, 'dw', data.details[i].unit_name);
                                $("#grid").jqGrid("setCell", i + 1, 'number', data.details[i].number);
                                $("#grid").jqGrid("setCell", i + 1, 'mark', data.details[i].mark);
                                $("#grid").jqGrid("setCell", i + 1, 'unit_id', data.details[i].unit_id);
                                $("#grid").jqGrid("setCell", i + 1, 'store_id', data.details[i].store_id);
                                $.get('/admin/basic/get_stock.html',
                                    {
                                        store_id: data.details[i].store_id,
                                        goods_id: data.details[i].goods_id
                                    }, function (result) {
                                        if (result.status == 1) {
                                            $('#grid').jqGrid('setCell', i + 1, 'use_ck', result.info);
                                        }
                                    });
                            }
                            // $("#choice_customer_name").val(data.info.names);
                            // $('#user_id').val(data.info.ids);
                        } else {
                            layer.msg("获取信息失败");
                        }
                    })
                }
                layer.close(index);
                return false;
            },
            btn4: function (index, layero) {
                layer.close(index);
                return false;
            }
        })
    }
    // 存为模板
    function saveTemplate() {
        content = '<div style="display: flex;align-items: center;height: 100%;padding-left:10px;"><div class="ui_content" style="padding: 10px;"><div class="re-initialize"><label for="tempName">模板名称:</label> <input type="text" id="templateName" name="templateName" class="ui-input" value=""></div></div></div>';
        layer.open({
            type: 1,
            title: "模板名称",
            area: ['280px', '220px'], //宽高
            content: content,
            btn: ['确定', '取消'],
            btn1: function (index, layero) {
                // var movedata = getJQAllData();
                // console.log(movedata);
                var data = getJQAllData($("#fixedGrid"));
                var sonData = getJQAllData($("#grid"));
                var templateName = $('#templateName').val();
                if (templateName == '') {
                    layer.msg('请先输入模板名称');
                    return false;
                }

                $.ajax({
                    type: 'POST',
                    url: "savetemplate.html",
                    data: { data: data, sonData: sonData, name: templateName, type: 0 },
                    dataType: "json",
                    async: false,
                    error: function (request) {
                        layer.msg("发送请求失败！");
                    },
                    success: function (data) {
                        if (data.status == 1) {
                            layer.msg(data.msg);
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });
                layer.close(index);
            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });
    }
</script>


{include file="admin@public/footer" /}